<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<div id="page-content">
    <div class="row">

        <div class="col-lg-12">
            <div class="panel">
                <div class="panel-heading">
                    <h3 class="panel-title">职权管理</h3>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-md-3">
                            <ul id="tree_department" class="ztree"></ul>
                        </div>
                        <div class="col-md-9">
                            <div class="form-inline">
                                <div class="row">
                                    <div class="col-sm-12 table-toolbar-left">
                                        <btn id="btn_add_power" class="btn btn-primary btn-sm">
                                            <i class="ti-plus"> </i> 添加职权
                                        </btn>
                                    </div>
                                </div>
                            </div>
                            <div class="jqGrid_wrapper">
                                <table id="power_table"></table>
                                <div id="power_page"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<script>

    $(function(){

        init_tree_department();

    });

    function init_tree_department() {

        var setting = {
            async: {
                enable: true,
                url:'<%=path%>/main/department/tree',
                autoParam: ['id'],
                type: 'get',
                dataType: 'json'
            },
            callback: {
                onAsyncSuccess: zTreeOnAsyncSuccess,
                onClick: zTreeOnClick
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: 'id',
                    pIdKey: 'pId',
                    rootPId: ''
                }
            },
            view: {
                autoCancelSelected: false,
                dblClickExpand: false,
                selectedMulti: false
            }
        };

        $.fn.zTree.init($('#tree_department'), setting);

    }

    function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
        var zTree = $.fn.zTree.getZTreeObj('tree_department');
        if (treeNode) {
            if (treeNode.id === '0') {
                var nodes = treeNode.children;
                zTree.selectNode(nodes[0]);
                init_grid_power(nodes[0].id);
            }
        } else {
            var rootNode = zTree.getNodes()[0];
            if (rootNode.isParent) {
                zTree.expandNode(rootNode, true, true, true);
            }
        }
    }

    function zTreeOnClick(event, treeId, treeNode){
        var id = treeNode.id;
        if (id) {
            $('#power_table').jqGrid('setGridParam', {datatype:'json',postData: {departmentId: id}, page: 1}).trigger('reloadGrid');
        }
    }

    function init_grid_power(departmentId) {

        __init_jqgrid('power_table', 'power_page',
                '<%=path%>/main/power/list_department',
                ['职权名称',  '上级职权', '操作', 'id'],
                [
                    {name: 'name', index: 'name', width: 60, sortable: true, stype: 'text', searchoptions: {sopt: ['cn']}},
                    {name: 'parentName', index: 'parentName', width: 100, sortable: true, search: true},
                    {name: 'act', index: 'act', width: 150, fixed: true, sortable: false, search: false, formatter: actFormat},
                    {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
                ],
                true,
                {
                    postData: {departmentId: departmentId}
                }
        );

    }

    function postFormat(cellvalue, options, rowObject){
        return rowObject.departmentName + ' - ' + rowObject.postName;
    }

    function typeFormat(cellvalue, options, rowObject){
        var id = rowObject.id;
        if (rowObject.type=='Principal') {
            cellvalue = '<span class="label label-warning">主</span>';
        } else {
            cellvalue = '<button class="btn btn-default btn-xs" data-original-title="设为主职" onclick="fn_power_method(\'Principal\', \'' + id + '\')"><i class="fa fa-star-o"> </i> 设为主职</button>';
        }

        return cellvalue;
    }

    function actFormat(cellvalue, options, rowObject){
        var id = rowObject.id;
        cellvalue = '<button class="btn btn-default btn-xs" data-original-title="编辑" onclick="fn_power_edit(\'' + id + '\');"><i class="fa fa-pencil-square-o"> </i> 编辑</button> ';
        cellvalue += '<button class="btn btn-danger btn-xs" data-original-title="删除" onclick="fn_power_method(\'Delete\',\'' + id + '\');"><i class="fa fa-trash-o"> </i> 删除</button>';

        return cellvalue;
    }

    $('#btn_add_power').click(function() {
        var zTree = $.fn.zTree.getZTreeObj('tree_department');
        var selectedNodes = zTree.getSelectedNodes();
        if (selectedNodes.length > 0) {
            var departmentId = selectedNodes[0].id;
            __open_dialog_form('新增职权', '<%=path%>/main/power/create?departmentId='+departmentId, function (dialogRef) {
                var callback = function (data) {
                    if (data) {
                        dialogRef.close();

                        // 刷新grid
                        $('#power_table').jqGrid().setGridParam({datatype:'json'}).trigger("reloadGrid");
                    } else {
                        dialogRef.enableButtons();
                    }
                };
                if (fn_power_input_save(callback) === false) {
                    dialogRef.enableButtons();
                }
            });
        } else {
            __toastr_warning('请选择一个部门');
        }
    });

    function fn_power_edit(id) {
        __open_dialog_form('编辑职权信息','<%=path%>/main/power/update/'+id, function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    // 刷新grid
                    $('#power_table').jqGrid().setGridParam({datatype:'json'}).trigger("reloadGrid");
                } else {
                    dialogRef.enableButtons();
                }
            };
            if (fn_power_input_save(callback) === false) {
                dialogRef.enableButtons();
            }
        });
    }

    function fn_power_method(method, id) {
        var message = '确定执行此操作么';
        if (method == 'Delete') {
            message = '确定删除么';
        } else if (method == 'Enable') {
            message = '确定启用么';
        } else if (method == 'Disenable') {
            message = '确定禁用么';
        } else if (method == 'Principal') {
            message = '确定设置为部门主职么';
        }
        __confirm_dialog(null, message,
                function () {
                    var url = '<%=path%>/main/power/method/'+method+'/'+id;
                    __ajax_post(url, null, function(data) {
                        __toastr(data);
                        if(data.state == 1) {
                            // 刷新grid
                            $('#power_table').jqGrid().setGridParam({datatype:'json'}).trigger("reloadGrid");

                        }
                    });
                }
        );
    }

</script>